import java.util.Arrays;
class Solution {

    private List<List<Integer>> result = new ArrayList<>();
    private List<Integer> otherArr = new ArrayList<>();
    private int[] nums;
    private boolean[] used;

    public List<List<Integer>> permute(int[] nums) {
        this.nums = nums;
        Arrays.sort(this.nums);
        this.used = new boolean[nums.length];
        putNumWithIndex(0);
        return result;
    }

    private void putNumWithIndex(int index) {
        if (index == this.nums.length) {
            this.result.add(new ArrayList<>(otherArr));
            return;
        }
        for (int i = 0; i < this.nums.length; i++) {
            if (i > 0 && this.nums[i] == this.nums[i - 1] && this.used[i - 1]) continue;
            if (!used[i]) {
                used[i] = true;
                otherArr.add(this.nums[i]);
                putNumWithIndex(index + 1);
                otherArr.remove(otherArr.size() - 1);
                used[i] = false;
            }
        }
    }

}
